package jdir.dialog;

import static jdir.util.LogUtil.begin;
import static jdir.util.LogUtil.end;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import jdir.Control;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Panel which holds the Userdefinedbuttons.
 * Currently BUTTON_ROW_COUNT and BUTTON_COL_COUNT are on fixed values.
 * @author Marco Peroverde
 */
@SuppressWarnings("unused")
public class UserButtonsPanel extends JPanel implements ActionListener
{
	private static Log				LOG			= LogFactory.getLog(UserButtonsPanel.class);
	private static Boolean			DEBUG		= LOG.isDebugEnabled();	
	private static Boolean			TRACE		= LOG.isTraceEnabled();
	
	private static final long	serialVersionUID	= 3954618731062264624L;
	
	//TODO Get this values from JDir settings
	private static final int BUTTON_ROW_COUNT = 3;
	private static final int BUTTON_COL_COUNT = 5;
	
	private JFrame parent;
	
	/**
	 * Initialice the UserButtonsPanel with 
	 * BUTTON_ROW_COUNT = 3;
	 * BUTTON_COL_COUNT = 5;
	 * Later this values will be set through configuration files
	 * @param parent Handle to parentframe
	 */
	public UserButtonsPanel(JFrame parent)
	{
		if(DEBUG)
			begin(LOG, "");
		this.parent = parent;
		this.setLayout(new GridLayout(BUTTON_ROW_COUNT,BUTTON_COL_COUNT));
		int buttonCount = BUTTON_ROW_COUNT * BUTTON_COL_COUNT;
		
		JButton[] buttons = new JButton[buttonCount];
		
		for(int i = 0; i < buttonCount; i++)
		{
			buttons[i] = new JButton(""+i);
			buttons[i].setActionCommand("UserButton-" + i);
			buttons[i].addActionListener(this);
			//buttons[i].setMaximumSize(new Dimension((Math.round(this.getWidth()/5)-buttonCount), 26));
		}
		
		for(int i = 0; i < buttonCount; i++)
			this.add(buttons[i]);
		
		this.setVisible(true);
		
		if(DEBUG)
			end(LOG, "");		
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		LOG.trace("Recieved event: " + e.getActionCommand());
		Object source = e.getSource();
		if(source.getClass() == new JButton().getClass())
		{
			Control.getInstance().performCommand(e.getActionCommand(), 0, (JDirMainDlg) parent);	
		}
		
	}

}
